package com.guang.springbootrequestversion.mappinghandlermapping;

import com.guang.springbootrequestversion.annotate.ApiVersion;
import com.guang.springbootrequestversion.condition.ApiVersionCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

import java.lang.reflect.Method;


@Slf4j
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    /**
     * 获取得到自定义的匹配类型
     * @param handlerType
     * @return
     */
    @Override
    protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
        return createCondition(AnnotationUtils.findAnnotation(handlerType,ApiVersion.class));
    }

    /**
     * 条件是什么
     * @param method
     * @return
     */
    @Override
    protected RequestCondition<?> getCustomMethodCondition(Method method) {
        return createCondition(AnnotationUtils.findAnnotation(method,ApiVersion.class));
    }

    private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) {
        log.info("开始构建请求条件({})",apiVersion);
        return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());
    }


}
